// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: MIT

package metrics

import (
	"os"
	"path/filepath"
	"testing"
)

func TestLoadConfig(t *testing.T) {
	dir := t.TempDir()
	cfgPath := filepath.Join(dir, "time-exporter.yaml")
	yamlContent := []byte("" +
		"ntp_config:\n" +
		"  ntp_server: test.pool.ntp.org\n" +
		"  ntp_protocol_version: 3\n" +
		"  ntp_server_is_local: true\n" +
		"  ntp_ip_ttl: 2\n" +
		"  ntp_max_distance: \"1.5s\"\n" +
		"  ntp_offset_tolerance: \"250ms\"\n" +
		"chrony_config:\n" +
		"  chrony_address: chrony.example.com\n" +
		"  chrony_port: 123\n")

	if err := os.WriteFile(cfgPath, yamlContent, 0644); err != nil {
		t.Fatalf("failed to write temp config: %v", err)
	}

	cfg := LoadConfig(cfgPath)
	if cfg.NTPConfig.NtpServer != "test.pool.ntp.org" {
		t.Fatalf("expected ntp server to be %q, got %q", "test.pool.ntp.org", cfg.NTPConfig.NtpServer)
	}
	if cfg.NTPConfig.NtpProtocolVersion != 3 {
		t.Fatalf("expected protocol version 3, got %d", cfg.NTPConfig.NtpProtocolVersion)
	}
	if cfg.NTPConfig.NtpServerIsLocal != true {
		t.Fatalf("expected server_is_local to be true")
	}
	if cfg.NTPConfig.NtpIPTTL != 2 {
		t.Fatalf("expected ip ttl 2, got %d", cfg.NTPConfig.NtpIPTTL)
	}
	if cfg.ChronyConfig.ChronyAddress != "chrony.example.com" {
		t.Fatalf("expected chrony address, got %q", cfg.ChronyConfig.ChronyAddress)
	}
	if cfg.ChronyConfig.ChronyPort != 123 {
		t.Fatalf("expected chrony port 123, got %d", cfg.ChronyConfig.ChronyPort)
	}
}
